<?php 
include ("../../inc/application_settings.php");
include(APPLICATION_COMMON);

/**
 * echos the html code for loginform
 */
function GenerateRegistrationForm(){	
	$mysql = new MYSQL(DB_NAME,DB_USER,DB_PASSWORD,DB_SERVER);
	try{
		$mysql->Connect();
		$password = GetLanguageSpecificText($mysql,"password_menue",LANGUAGE);
		$firstname = GetLanguageSpecificText($mysql,"firstname_registration",LANGUAGE);
		$lastname = GetLanguageSpecificText($mysql,"lastname_registration",LANGUAGE);
		$gebDate = GetLanguageSpecificText($mysql,"birthday_registration",LANGUAGE);
		$passwordrep = GetLanguageSpecificText($mysql,"passwordrep_registration",LANGUAGE);
		$nameError = GetLanguageSpecificText($mysql,"name_registration_error",LANGUAGE);
		$gegDateError = GetLanguageSpecificText($mysql,"gebdate_registration_error",LANGUAGE);
		$emailError = GetLanguageSpecificText($mysql,"email_registration_error",LANGUAGE);
		$passwordError = GetLanguageSpecificText($mysql,"password_registration_error",LANGUAGE);
		$registrationSuccess = GetLanguageSpecificText($mysql,"success_registration",LANGUAGE);
		$registrationError = GetLanguageSpecificText($mysql,"registration_error",LANGUAGE);
		
		$mysql->Disconnect();
	} catch (Exception $e){
		echo $e->getMessage();
		return;
	}
	
	$output = '<form>';
	$output .=	'<b>'.$firstname.'</b>&nbsp;<span id="firstname_message" class="regErrorMessage">('.$nameError.')</span><br />';
	$output .=	'<input type="text" name="firstname" id="firstname" class="text ui-widget-content ui-corner-all" style="width:100%" /><br /><br />';
	$output .=	'<b>'.$lastname.'</b>&nbsp;<span id="lastname_message" class="regErrorMessage">('.$nameError.')</span><br />';
	$output .=	'<input type="text" name="lastname" id="lastname" class="text ui-widget-content ui-corner-all" style="width:100%" /><br /><br />';
	$output .=	'<b>'.$gebDate.'</b>&nbsp;<span id="gebDate_message" class="regErrorMessage">('.$gegDateError.')</span><br />';
	$output .=	'<input type="text" name="gebDate" id="gebDate" class="text ui-widget-content ui-corner-all" style="width:100%;" readonly="readonly" value=" "/><br /><br />';
	$output .=	'<b>Email</b>&nbsp;<span id="email_message" class="regErrorMessage">('.$emailError.')</span><br />';
	$output .=	'<input type="text" name="email" id="email" class="text ui-widget-content ui-corner-all" style="width:100%" /><br /><br />';
	$output .=	'<b>'.$password.'</b>&nbsp;<span id="password_message" class="regErrorMessage">('.$passwordError.')</span><br />';
	$output .=	'<input type="password" name="password" id="password" class="text ui-widget-content ui-corner-all" style="width:100%" /><br /><br />';
	$output .=	'<b>'.$passwordrep.'</b>&nbsp;<span id="passwordrep_message" class="regErrorMessage">('.$passwordError.')</span><br />';
	$output .=	'<input type="password" name="passwordrep" id="passwordrep" class="text ui-widget-content ui-corner-all" style="width:100%" /><br />';
	$output .=	'</form>';
	$output .=  '<div class="ui-widget" style="display:none" id="registrationSuccess"><div class="ui-state-highlight ui-corner-all" style="margin-top: 20px; padding: 0 .7em;"><p><span class="ui-icon ui-icon-check" style="float: left; margin-right: .3em;"></span>'.$registrationSuccess.'!</p></div></div>';
	$output .=  '<div class="ui-widget" style="display:none" id="registrationError"><div class="ui-state-error ui-corner-all" style="margin-top: 20px; padding: 0 .7em;"><p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>'.$registrationError.'!</p></div></div>';
	echo $output;

}


/**
 * handles the registration for one person
 */
function Registrate(){
	$firstname = (isset($_REQUEST['FIRSTNAME']))?$_REQUEST['FIRSTNAME']:null;
	$lastname = (isset($_REQUEST['LASTNAME']))?$_REQUEST['LASTNAME']:null;
	$gebDate = (isset($_REQUEST['GEBDATE']))?$_REQUEST['GEBDATE']:null;
	$email = (isset($_POST['EMAIL']))?$_POST['EMAIL']:null;
	$password = (isset($_POST['PASSWORD']))?$_POST['PASSWORD']:null;
	
	if ($firstname and $lastname and $gebDate and $email and $password){
		$mysql = new MYSQL(DB_NAME,DB_USER,DB_PASSWORD,DB_SERVER);
		try{
			$mysql->Connect();
			$sql = 'insert into '.DB.'.Person (person_id, firstname,lastname,gebdate,email,password,telNr,address) values (?,?,?,cast(? as date),?,?,?,?)';
			$gebDate = date_create($gebDate);
			$gebDate = date_format($gebDate, 'Y-m-d');
			$params = array(
				GetNextID($mysql,DB.'.Person','person_id'),
				$firstname,
				$lastname,
				$gebDate,
				$email,
				$password,
				'NULL',
				'NULL');
			$mysql->Execute($sql,$params);
			$mysql->Disconnect();
			
			$ret = array('RESPONSE'=>'SUCCESS');
		}catch(Exception $e){
			$ret = array('RESPONSE'=>'ERROR','MESSAGE'=>$e->GetMessage());
		}
		
	}else
		$ret = array('RESPONSE'=>'ERROR', 'MESSAGE'=>'Registration attributes are not set');
	
	
	echo json_encode($ret);
}





$target = (isset($_REQUEST['T']))?$_REQUEST['T']:NULL;
switch($target){
	case 'REGISTRATIONFORM':
		GenerateRegistrationForm();
		break;
	case 'REGISTRATE':
		Registrate();
		break;
}
?>